import 'package:class_teacher_student/model/Letter.dart';
import 'package:class_teacher_student/model/section.dart';
import 'package:class_teacher_student/extension/global_key_extension.dart';
import 'package:class_teacher_student/model/student.dart';
import 'package:class_teacher_student/page/cts_list_page.dart';
import 'package:class_teacher_student/extension/list_section_extension.dart';
import 'package:flutter/material.dart';

import 'sticky_head.dart';

class AlphabetList extends StatefulWidget {
  List<Section> sections;
  ScrollController contentController;
  GlobalKey sectionListKey;
  GlobalKey stickyHeadKey;

  AlphabetList(List<Section> this.sections,
      ScrollController this.contentController,
      GlobalKey this.sectionListKey,
      GlobalKey this.stickyHeadKey,
      {Key key} ):super(key: key);

  @override
  AlphabetListState createState() => AlphabetListState();
}

class AlphabetListState extends State<AlphabetList> {

  static String currentChacacter='A';

  bool isChanged(){
    return StickyHeadState.currentCharacter != currentChacacter;
  }

  @override
  Widget build(BuildContext context) {
    List<Letter> letters = Letter.getLetters();
    List<Section> sections = widget.sections;
    ScrollController contentController = widget.contentController;

    return Positioned(
        top: 60,
        right: 20,
        bottom: 20,
        width: 15,
        child:ListView.builder(
            itemCount: letters.length,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                child: Container(
                    height: 20,
                    width: 15,
                    child:Text(
                      letters[index].character,
                      style: currentChacacter==letters[index].character ? TextStyle(color: Colors.black, fontWeight: FontWeight.bold) : TextStyle(color: Colors.grey),
                    )
                ),
                onTap: () {
                  double offsetY = sections.getScrollOffsetByAlphabetIndex(index);
                  letters[index].isSelected=true;
                  currentChacacter=letters[index].character;

                  //jumpTo跳转函数会触发滑动监听，重复刷新字母列表/黏头
                  //在跳转前关闭监听，刷新完字母列表/黏头后，再启动监听
                  CtsListPageState.isScrollListenerDummy=true;
                  contentController.jumpTo(offsetY);
                  StickyHeadState.currentIndex=Letter.getLetters()[index].indexOfSection;
                  StickyHeadState.currentCharacter=currentChacacter;
                  widget.stickyHeadKey.refresh();
                  setState(() {
                  });
                  Future.delayed(Duration(microseconds: 2000))
                    .then((_){CtsListPageState.isScrollListenerDummy=false;});
                },
              );
            }
        )
    );
  }
}

extension AlphabetListKey on GlobalKey<AlphabetListState>{
  void refresh() {
    if(this.currentState.isChanged()) {
      this.currentState.setState(() {
        AlphabetListState.currentChacacter = StickyHeadState.currentCharacter;
      });
    }
  }
}
